package com.swipemaster.game.modes;

import android.app.Activity;

import com.google.analytics.tracking.android.EasyTracker;
import com.swipemaster.R;
import com.swipemaster.activities.GameActivity;
import com.swipemaster.common.Const;
import com.swipemaster.common.Global;
import com.swipemaster.common.Prefs;
import com.swipemaster.common.Track;
import com.swipemaster.dialogs.Dialogs;
import com.swipemaster.game.GameSurface;
import com.swipemaster.levels.Level;
import com.swipemaster.levels.LevelFactory;
import com.swipemaster.levels.levelinfo.LevelInfo;
import com.swipemaster.record.IntegerRecord;
import com.swipemaster.record.PuzzleRecord;
import com.swipemaster.record.Record;
import com.swipemaster.record.RecordId;
import com.swipemaster.record.RecordsManager;
import com.swipemaster.record.SurvivalRecord;

public class LevelsMode extends GameMode
{
	protected int levelIndex;
	private static boolean isHelpHidden[] =
	{ Global.app.prefs.getBoolean(Prefs.FIRST_HELP_KEY, false),
			Global.app.prefs.getBoolean(Prefs.SECOND_HELP_KEY, false) };

	public LevelsMode(int levelIndex)
	{
		super(new RecordId(levelIndex));
		this.levelIndex = levelIndex;
	}

	public LevelsMode(RecordId recordId)
	{
		super(recordId);
	}

	@Override
	public void addModePlayed(boolean isWinner)
	{
		super.addModePlayed(isWinner);

		final IntegerRecord record = (IntegerRecord) RecordsManager.getRecord(RecordId.LEVEL_GAMES_PLAYED);
		record.counter++;
		RecordsManager.getInfo(levelIndex).playedTimes++;

		EasyTracker.getTracker().sendView(Track.getLevelDimension(levelIndex));
	}

	@Override
	public Level getNewLevel()
	{
		return LevelFactory.getLevel(levelIndex);
	}

	@Override
	public int getPlayedTimes()
	{
		return RecordsManager.getInfo(levelIndex).playedTimes;
	}

	@Override
	public Record getRecord()
	{
		SurvivalRecord record;
		if (RecordsManager.getInfo(levelIndex).isPuzzle)
		{
			record = new PuzzleRecord(levelIndex);
		}
		else
		{
			record = new SurvivalRecord(levelIndex);
		}
		record.time = GameSurface.level.getPlayingTime();
		return record;
	}

	@Override
	public String getYourScoreText()
	{
		return Global.app.getString(R.string.your_time);
	}

	@Override
	public void hideHelp()
	{
		isHelpHidden[levelIndex] = true;
		if (levelIndex == 0)
		{
			Global.app.prefs.putObject(Prefs.FIRST_HELP_KEY, true);
		}
		else
		{
			Global.app.prefs.putObject(Prefs.SECOND_HELP_KEY, true);
		}
	}

	@Override
	public boolean isTimeConstraint()
	{
		return true;
	}

	@Override
	public boolean mustWinToUpdate()
	{
		if (RecordsManager.getInfo(levelIndex).isPuzzle)
		{
			return true;
		}
		return false;
	}

	@Override
	public void onNext()
	{
		levelIndex++;
		recordId.advanceLevelIndex();
	}

	@Override
	public void playSong()
	{
		Global.app.musicPlayer.playLevelSong(levelIndex);
	}

	@Override
	public void showHelp(Activity activity)
	{
		if (isHelpNecessary(levelIndex))
		{
			Dialogs.showHelp(activity, recordId);
		}
	}

	@Override
	boolean isNextButton(boolean isWinner)
	{
		final int next = levelIndex + 1;

		if (isWinner)
		{
			RecordsManager.getInfo(levelIndex).isCompleted = true;
			RecordsManager.addStarsForLevel(levelIndex);
		}

		if (next >= Const.LEVELS)
		{
			return false;
		}

		final LevelInfo levelInfo = RecordsManager.getInfo(next);
		return !levelInfo.isLocked && levelInfo.isFree;
	}

	protected boolean isHelpNecessary(int levelIndex)
	{
		return (levelIndex < 2) && !isHelpHidden[levelIndex];
	}

	@Override
	public void onGameOver(GameActivity activity, boolean isWinner)
	{
		if (isWinner)
		{
			if (levelIndex == 0)
			{
				RecordsManager.getInfo(1).isLocked = false;
			}
			if (levelIndex == 1)
			{
				RecordsManager.getInfo(2).isLocked = false;
				RecordsManager.getInfo(3).isLocked = false;
			}
		}

		super.onGameOver(activity, isWinner);

		if (RecordsManager.getInfo(0).isCompleted && RecordsManager.getInfo(1).isCompleted)
		{
			final int stars = RecordsManager.getStars();
			RecordsManager.unlockLevelsForStars(stars);
		}
	}
}
